<?php

declare(strict_types=1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use think\facade\Config;
use think\facade\Db;

class Structure extends Base
{
    // 历史构建信息
    public function index()
    {
        $datalist = [
            [
                'id'            => 1,
                'title'         => '标题111',
                'category'      => '分类名称',
                'status'        => '正常',
                'view'          => 0,
            ],
            [
                'id'            => 1,
                'title'         => '标题111',
                'category'      => '分类名称',
                'status'        => '正常',
                'view'          => 0,
            ],
            [
                'id'            => 1,
                'title'         => '标题111',
                'category'      => '分类名称',
                'status'        => '正常',
                'view'          => 0,
            ],
        ];
        $builder = new \hangpu8\hpcms\Utils\Builder\ListBuilder;
        $data = $builder
            ->addTopButton('structureCode', '快速构建', [
                'title'         => '快速构建功能',
                'api'        => 'hangpu8/v1.Structure/add',
                'pageType'      => 'structure'
            ])
            ->addColumn('id', '序号')
            ->addColumn('title', '标题')
            ->addColumn('category', '分类')
            ->addColumn('status', '状态')
            ->addColumn('view', '浏览次数')
            ->setData($datalist)
            ->create();
        return Json::successful($data);
    }

    /**
     * 构建代码
     *
     * @return void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = request()->post();
            // 获取参数
            $tableName = (string)$post['tableName'];
            $tableRemarks = (string)$post['tableRemarks'];
            $appModule = (string)$post['appModule'];
            $extendName = (string)$post['extendName'];
            $version = (string)$post['version'];
            $tableData = (array)$post['tableData'];
            $serviceData = (array)$post['serviceData'];
            $validateData = (array)$post['validateData'];
            // 处理数据
            try {
                $service = new \hangpu8\hpcms\service\CrudService($tableName, $appModule, $tableRemarks, $extendName, $version);
                $service->createController($serviceData);
                $service->createModel();
                $service->createValidateModel($validateData);
                $service->createTables($tableData);
                $model = new \hangpu8\hpcms\model\Structure;
                $data['ctime']              = date('Y-m-d H:i:s');
                $data['table_name']         = $tableName;
                $data['module_name']        = $appModule;
                $data['remarks']            = $tableRemarks;
                $data['extend_name']        = $extendName;
                $data['version']            = $version;
                $data['params']             = $post;
                $model->save($data);
                return Json::successful([], '恭喜您，构建功能完成');
            } catch (\Exception $e) {
                return Json::fail($e->getMessage());
            }
        } else {
            $builder = new \hangpu8\hpcms\Utils\Builder\GroupForm;
            $data = $builder
                ->create();
            $data['appModule'] = array_values(Config::get("app.app_map"));
            return Json::successful($data, 'success');
        }
    }

    // 删除构建信息
    public function del()
    {
        return '删除构建信息';
    }
}
